<template>
  <div class="clearDiff">
    <basic-container>
      <avue-crud
        ref="crud"
        :option="option"
        :table-loading="listLoading"
        :data="list"
        :page.sync="page"
        @search-change="handleFilter"
        @selection-change="selectionChange"
        :show-column.sync="showColumn"
        @on-load="getList">
        <template slot-scope="scope" slot="diffSource">
          <router-link to="/subwayrca/clear-system/report-view/sw-ck-clearing-report-oneway-ticket/index"><el-tag size="medium"  v-if="scope.row.diffSource == 1" >单程票-应收-实收统计</el-tag></router-link>
          <router-link to="/subwayrca/clear-system/report-view/sw-ck-clearing-report-qr/index"><el-tag size="medium"  v-if="scope.row.diffSource == 2">二维码应收-应付差异统计</el-tag></router-link>
          <router-link to="/subwayrca/clear-system/report-view/sw-ck-clearing-account-repport-face/index"><el-tag size="medium"  v-if="scope.row.diffSource == 3">人脸电子票应收应付差异统计</el-tag></router-link>
        </template>
        <template slot="searchMenu" slot-scope="{row,size}">
          <el-button :size="size" @click="()=>{setSearchFiledsEmpty(row);setSearchFiledsEmpty(searchForm);searchForm.diffSource = 1}" icon="el-icon-delete">清空</el-button>
        </template>
        <template slot-scope="scope" slot="diffSourceSearch">
          <el-select v-model="searchForm.diffSource" placeholder="报表名称" style="width: 200px;" suffix-icon="" clearable filterable>
            <el-option
              :selected="searchForm.diffSource"
              v-for="(item,index) in diffSourceCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="diffReasonTimeSearch">
          <avue-date v-model="searchForm.searchDate" format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="请选择日期"></avue-date>
        </template>
        <template slot-scope="scope" slot="sendStatusSearch">
          <el-select v-model="searchForm.sendStatus" placeholder="下发状态" style="width: 150px" clearable filterable>
            <el-option
              v-for="(item,index) in sendStatusCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot="menuRight">
          <el-button type="primary"  size="mini" @click="writeDesc">完善差异原因</el-button>
          <el-button type="primary"  size="mini" @click="sendDiff">下发</el-button>
          <!--<el-button type="primary"  size="mini" @click="sendDiff">导入</el-button>-->
          <el-upload
            class="upload-inline"
            action=""
            :show-file-list="false"
            accept=".xls,.xlsx"
            :http-request="handleImport"
          >
            <el-button type="primary" size="mini">导入</el-button>
          </el-upload>
          <!-- <Export style="display: inline-flex;padding: 0px;width: auto"></Export> -->
          <el-button
            size="mini"
            type="primary"
            @click="exportClick()"
          >导出</el-button>
        </template>
      </avue-crud>
    </basic-container>
    <mutilClear ref="mutilClear" v-if="isShowMutilCleardDiff"></mutilClear>
  </div>

</template>

<script>
  import {getForm, postForm} from '@/api/gen/gen'
  import {mapGetters} from 'vuex';
  import {exportDataToExcel} from "../../../api/subwayRat/exportSelf";
  import mutilClear from './mutilCleardDiff';
  import Export from './ExportDiff.vue';
  import { handleExcelResponse } from "@/util/util";
  import {
    getTransferDiffList,
    setSearchFiledsEmpty,
    tableOption,
    saveMutilCleardDiff,
    sendMutilCleardDiff,
    getExcelList,
    dataExport,
    saveDiffBatchList
  } from "../../../api/subwayRat/cleardiff/cleardiff";
  export default {
    name: 'clearDiff',
    components:{
      mutilClear,
      Export
    },
    data() {
      return {
        isShowDia:false,
        isShowMutilCleardDiff:false,
        option: tableOption,
        listLoading: true,
        selectKeys:[], //存放对像
        selectKeyVals:[], //存放值
        showColumn:[],
        diffSourceCodes:[{'code':1,'name':'单程票-应收-实收统计'},
          {'code':2,'name':"二维码应收-应付差异统计"},
          {'code':3,'name':'人脸电子票应收应付差异统计'}],//1. 单程票-应收-实收统计 2.二维码应收-应付差异统计 3.	人脸电子票应收应付差异统计
        sendStatusCodes:[{'code':1,'name':'已下发'},{'code':2,'name':'未下发'}],//1：已下发 0：未下发
        searchForm:{
          searchDate:'',
          diffSource:1,
          sendStatus:'',
        },
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20, // 每页显示多少条,
          isAsc: false// 是否倒序
        },
        list : [],
        userList:[],
        showType : 0,
        equipmentNumberList : [],
      }
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    watch: {
    },
    mounted() {
        this.searchDiffSource();
    },
    created() {

    },
    methods: {
      setSearchFiledsEmpty,
      async writeDesc(){
        if(this.selectKeys.length<=0){
          this.$message.warning("请选择要回馈的数据！");
          return;
        }
        for(let i=0;i<this.selectKeys.length;i++){
          if(1 == this.selectKeys[i].lineDataStatus){
            this.$message.warning("勾选数据中存在已回馈数据！");
            return
          }
        }
        this.isShowMutilCleardDiff =true;
        let p = {
            ids:JSON.stringify(this.selectKeyVals)
        };
//        await updateSwStTicketInformationCheck(p).then((response)=>{
//        });
//        this.getList();
      },
      async sendDiff(){
        if(this.selectKeys.length<=0){
          this.$message.warning("请选择要回馈的数据！");
          return;
        }
        for(let i=0;i<this.selectKeys.length;i++){
          if(1 == this.selectKeys[i].lineDataStatus){
            this.$message.warning("勾选数据中存在已回馈数据！");
            return
          }
        }
        let p = {
          ids:JSON.stringify(this.selectKeyVals)
        };
        sendMutilCleardDiff(JSON.stringify(p)).then((response)=>{
          this.getList();
        });
      },
      searchDiffSource(){
        if('1' == this.searchForm.diffSource){
          this.showColumn=['diffSource','rediffTradeDate','diffReasonTime','alrealIncomeMoney','reaccReceivableMoney','reaccPayableMoney','rediffAccRealMoney','rediffAccReceivableMoney','createTime','replayLimit','isInTime','diffReason','sendStatus'];
        }else if('2' == this.searchForm.diffSource){
          this.showColumn=['diffSource','qrdiffTradeDate','diffReasonTime','alrealIncomeMoney','alaccMoney','aldiffAccMoney','createTime','replayLimit','isInTime','diffReason','sendStatus'];
        }else if('3' == this.searchForm.diffSource){
          this.showColumn=['diffSource','acdiffTradeDate','diffReasonTime','alrealIncomeMoney','alaccMoney','aldiffAccMoney','createTime','replayLimit','isInTime','diffReason','sendStatus'];
        }
      },
      // async exportData(){ // 导出
      //   this.searchForm.pageNo=1;
      //   this.searchForm.pageSize=1000000000;
      //   if(this.selectKeys.length>0){
      //     exportDataToExcel(this.selectKeys,this.option.column,'系统报表清分差异原因');
      //   }else {
      //     let res=await getExcelList(this.searchForm);
      //     let data = res.data.data;
      //     exportDataToExcel(data,this.option.column,'系统报表清分差异原因');
      //   }
      // },
      handleImport(item) {//导入
        let file = item.file;
        let param = new FormData();
        param.append("file", file);
        saveDiffBatchList(param).then(() => {
          this.$notify({
            title: "成功",
            message: "导入成功",
            type: "success",
            duration: 2000
          });
          this.getList();
        });
      },
      //导出
      async exportClick (row) {
        this.dataExport();
      },
      async dataExport () {
        let loading = this.$loading({
          lock: true,
          text: "Loading",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)"
        })
        try {
          if(this.selectKeys.length>0){
            debugger
            const res = await dataExport(Object.assign({ids:this.selectKeyVals.join(",")},this.searchForm));
            handleExcelResponse(res);
          }else{
            debugger
            const res = await dataExport(Object.assign(this.searchForm));
            handleExcelResponse(res);
          }
        } finally {
          loading.close();
        }
        
      },
      previewEditor(row,type){
        this.showType = type;
        this.isShowDia=true;
        this.$nextTick(()=>{
//          this.$refs.HuikuiInfo.recallShow(row);
        })
      },
      getList() {
        this.searchDiffSource();
        this.listLoading = true;
        Object.assign(this.searchForm,this.page);
        getTransferDiffList(this.searchForm).then((response)=>{
          this.list = response.data.data;
          this.listLoading = false;
          this.page.total=response.data.data.total;
        });
      },
      selectionChange(list){
        this.selectKeys=list;
        this.selectKeyVals=[];
        for(let i=0;i<list.length;i++){
          this.selectKeyVals.push(list[i].id);
        }
      },
      doMutilCleardDiff(parm){
        saveMutilCleardDiff(JSON.stringify(parm)).then((response)=>{
          this.getList();
        });
      },
      handleFilter(params, done) {
        Object.assign(this.searchForm,params);
        this.getList();
        done()
      }
    }
  }
</script>
